//
//  StatementViewController.m
//  Thinkape-iOS
//
//  Created by 刚刚买的电脑 on 15/11/18.
//  Copyright © 2015年 TIXA. All rights reserved.
//

#import "StatementViewController.h"
#import "AppDelegate.h"
#import "DataManager.h"
@interface StatementViewController ()<UIWebViewDelegate>

@property (nonatomic,strong)UIAlertController *alertController;
@property (nonatomic,copy)NSString *uid;
@property (nonatomic,strong)AppDelegate *delegate;
@property (nonatomic, assign)NSInteger finish;//退出的时候竖屏
@end

@implementation StatementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报表";
    self.view.backgroundColor = [UIColor whiteColor];
    self.webView.scalesPageToFit = YES;
    self.webView.delegate = self;
    self.webView.opaque = NO;
    self.webView.scrollView.bounces = NO;
    
    self.uid = [DataManager shareManager].uid;
   
    _delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    _delegate.allowRotation = 1;
    
    [self createBackBtn];
    
    
//    NSURL *url0 = [[NSBundle mainBundle]URLForResource:@"test" withExtension:@"html"];
    
    NSString *str0 = [DataManager shareManager].webDomain;
//    NSString *urlStr = @"%@/Mobile/report/report_lead.html";
    NSString *urlStr = [NSString stringWithFormat:@"%@/Mobile/report/report_lead.html",str0];
    //http://27.115.23.126:5032/Mobile/report/report_lead.html
//    NSString *url = [NSString stringWithFormat:@"%@/Mobile/report/report_lead.html",[DataManager shareManager].webDomain];
    
    NSLog(@"url : %@",urlStr);
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    

}
- (void)createBackBtn
{
    UIButton *backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setImage:[UIImage imageNamed:@"navi_back"] forState:UIControlStateNormal];
    [backBt setFrame:CGRectMake(20, 10, 70, 44)];
    [backBt setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 40)];
    [backBt addTarget:self action:@selector(Goback) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backBt];
    self.navigationItem.leftBarButtonItem = back;

}

- (void)Goback
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark  UIWebViewDelegate

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //通过模型调用
    YLJsObjcModel *model = [[YLJsObjcModel alloc]init];
    self.context[@"tran"] = model;
    
    model.context0 = self.context;
    model.webView0 = self.webView;

    model.uid = self.uid;
    model.delegate = self;
    
//    #idepid

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dict = [defaults objectForKey:@"dicerv"];
    NSString *idepid = [dict objectForKey:@"#idepid"];
    model.idepid = idepid;
    
    
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue){
        
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //通过模型调用
    YLJsObjcModel *model = [[YLJsObjcModel alloc]init];
    self.context[@"tran"] = model;
    
    model.context0 = self.context;
    model.webView0 = self.webView;
    model.uid = self.uid;
    model.delegate = self;
    
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue){
        
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };

}


-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error
{
    [self createAlertController];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    static BOOL isRequest = YES;
    
    if (isRequest) {
        NSHTTPURLResponse *response = nil;
        
        NSData *data = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:nil];
        
        if (response.statusCode == 404) {

            //提示用户webView出错：
            [self createAlertController];
            return NO;
        } else if (response.statusCode == 403) {

            [self createAlertController];
            return NO;
            
        }
        
        [webView loadData:data MIMEType:@"text/html" textEncodingName:nil baseURL:[request URL]];
        
        isRequest = NO;
        return NO;
    }
    
    return YES;
}

//返回上一级VC：
- (void)closeVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

//创建提示框：
- (void)createAlertController
{
    _alertController = [UIAlertController alertControllerWithTitle:@"请求出错" message:@"暂时没有请求到报表数据，请稍后再试" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self closeVC];
    }];
                             
    [_alertController addAction:action];
    
    [self presentViewController:_alertController animated:YES completion:nil];

}


-(void)viewWillAppear:(BOOL)animated
{
    _delegate.allowRotation = 0;
    
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
